package com.kilomob.grady.proxy;


import com.kilomob.grady.common.Constants;
import com.kilomob.grady.exception.GradyException;
import com.kilomob.grady.rpc.Invoker;
import com.kilomob.grady.utils.ReflectUtils;

public abstract class AbstractProxyFactory implements ProxyFactory {

    public <T> T getProxy(Invoker<T> invoker,String config) throws GradyException {
        Class<?>[] interfaces = null;
        invoker.getInterface();
        if (config != null && config.length() > 0) {
            String[] types = Constants.COMMA_SPLIT_PATTERN.split(config);
            if (types != null && types.length > 0) {
                interfaces = new Class<?>[types.length + 1];
                interfaces[0] = invoker.getInterface();
                for (int i = 0; i < types.length; i ++) {
                    interfaces[i + 1] = ReflectUtils.forName(types[i]);
                }
            }
        }
        if (interfaces == null) {
            interfaces = new Class<?>[] {invoker.getInterface()};
        }
        return getProxy(invoker, interfaces);
    }
    
    public abstract <T> T getProxy(Invoker<T> invoker, Class<?>[] types);

}